package com.mobile.liuyi_s.orderlunch.Utils;

import android.app.Activity;
import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.text.format.Time;
import android.util.Log;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.mobile.liuyi_s.orderlunch.entity.Order;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Created by liuyi_s on 2017/7/27 15:43.
 * Email：liuyi_s@foxmail.com
 */

public class Utils {
    public static void hideKeyBoard(Activity activity, InputMethodManager inputMethodManager) {
        if (activity.getWindow().getAttributes().softInputMode !=
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (activity.getCurrentFocus() != null) {
                inputMethodManager.hideSoftInputFromWindow(
                        activity.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    public static String getInternetTime() {
        URL url = null;
        String time = null;
        try {
            url = new URL("http://www.ntsc.ac.cn");
            URLConnection connection = url.openConnection();
            connection.connect();
            long longTime = connection.getDate();
            Date date = new Date(longTime);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
                time = sdf.format(date);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return time;
    }
    public boolean isInOrderTime() {
        boolean isInOrderTime = false;
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour < 10) {
            isInOrderTime = true;
        }
        return isInOrderTime;
    }

    public int getLocacYear() {
        int year = 0;
        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        return year;
    }
    public int getLocacMonth() {
        int month = 0;
        Calendar calendar = Calendar.getInstance();
        month = calendar.get(Calendar.MONTH) + 1;
        return month;
    }
    public int getLocacDay() {
        int day = 0;
        Calendar calendar = Calendar.getInstance();
        day = calendar.get(Calendar.DAY_OF_MONTH);
        return day;
    }

    public int getOrderNumInList(List<Order> list) {
        int sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum +=list.get(i).getOrderNum();
        }
        return sum;
    }

}
